package com.lczyfz.istep.common.utils;


import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hpp on 2017/3/31.
 */
public class BaiduMapUtils {

    //百度地图开发者密钥
    private static final String USER_KEY = "iQSOH9gSTp3jPQX12j9dBPPhvpqm3rjN";

    /**
     * 坐标转换
     *
     * @param longitude
     * @param latitude
     * @return status locationX locationY
     * Reference ：http://lbsyun.baidu.com/index.php?title=webapi/guide/changeposition
     */
    public static Map<String, Object> coordinateTransformation(String longitude, String latitude) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(longitude) && StringUtils.isNotBlank(latitude)) {
            String paras = longitude + "," + latitude;
            String url = "http://api.map.baidu.com/geoconv/v1/?"
                    + "coords=" + paras
                    + "&from=1"
                    + "&to=5"
                    + "&ak=" + USER_KEY;
            String httpResult = HttpClientUtils.sendHttpGet(url);

            JSONObject jsonObject = JSONObject.fromObject(httpResult);
            String status = jsonObject.getString("status");
            String result = jsonObject.getString("result").replace("[", "").replace("]", "");
            JSONObject resultObject = JSONObject.fromObject(result);
            String locationX = resultObject.getString("x");
            String locationY = resultObject.getString("y");
            map.put("status", status);
            map.put("locationX", locationX);
            map.put("locationY", locationY);
            System.out.println("map=" + map);
        }
        return map;
    }


}
